<?php

namespace Backend\Modules\Video\Controllers;

use Common\library\Common;
use Library\Constant\Constant;
//use Phalcon\Paginator\Adapter\Model as Paginator;
use Phalcon\Paginator\Adapter\QueryBuilder as Paginator;
use Phalcon\Mvc\Model\Resultset;
use Library\Pagination\Pager as Pager;
use Backend\Modules\Video\Models\Video;
use Backend\Modules\Video\Models\Artist;
use Backend\Modules\Video\Forms\FormSearchArtist;

class AjaxVideoArtistController extends \BackendController
{
	public function loadArtistAction()
	{
		$this->view->setTemplateAfter('empty');

        $currentPage = abs($this->request->getPost('page', 'int', 1));
        $artistType = $this->request->get('type') ? $this->request->get('type') : Artist::ACTOR;

        $artists = new Artist();
        $artists = $artists->queryBuilder(array('artist_type' => $artistType));

        if ($currentPage == 0) {
            $currentPage = 1;
        }

        $pager = new Pager(
            new Paginator(array(
                'builder'  => $artists,
                'limit' => Constant::ITEM_PER_PAGE_20,
                'page'  => $currentPage,
            )),
            array(
                'layoutClass' => 'Library\Pagination\Pager\Layout\Bootstrap',
                'rangeLength' => 5,
                'urlMask'     => '&page={%page_number}',
            )
        );
        $this->view->pager = $pager;
        $this->view->artistType = $artistType;
        $this->view->objectId = $this->request->get('object_id');
        $this->view->objectType = $this->request->get('object_type');
        $this->view->formSearchArtist = new FormSearchArtist();
        $this->view->render("ajax-video-artist", "modal/modal_artist");
	}

	public function searchArtistAction()
	{
		$json['success'] = false;

        if($this->request->isAjax())
        {
            $this->view->setTemplateAfter('empty');

            $artistType = $this->request->getPost('artist_type') ? $this->request->get('artist_type') : Artist::ACTOR;
            $currentPage = abs($this->request->getPost('page', 'int', 1));
            
            $conditions = $this->request->getPost();
            $artists = new Artist();
            $artistsBuilder = $artists->queryBuilder($conditions);

            if ($currentPage == 0) {
                $currentPage = 1;
            }

            $pager = new Pager(
                new Paginator(array(
                    'builder'  => $artistsBuilder,
                    'limit' => Constant::ITEM_PER_PAGE_20,
                    'page'  => $currentPage,
                )),
                array(
                    'layoutClass' => 'Library\Pagination\Pager\Layout\Bootstrap',
                    'rangeLength' => 5,
                    'urlMask'     => '&page={%page_number}',
                )
            );
            $this->view->pager = $pager;
            $this->view->render("ajax-video-artist", "result_search_artist");

            $json['success'] = true;
            $json['html'] = $this->view->getContent();
            $this->response->setContent(json_encode($json));
            //Return the response
            return $this->response->send();
        }
        echo json_encode($json);
	}

	public function addArtistToObjectAction()
	{
		if(!$this->request->has('object_type'))
        {
            return ;
        }
        $objectType  = $this->request->get('object_type');

        switch($objectType)
        {
            case 'video':
               	if($this->request->getPost('selectArtist'))
                {
                	$id = $this->request->get('object_id');
                	
                 	$video = \Backend\Modules\Video\Models\Video::findFirstById($id);
                    if(!$video)
                    {
                        return $this->response->redirect($this->request->getHTTPReferer());
                    }

                    $selectArtists = $this->request->getPost('selectArtist');

                    $data = array();
                    foreach($selectArtists as $artistId)
                    {
                        $checkArtistHaveVideo = \Backend\Modules\Video\Models\VideoArtistLink::find(array(
                            "video_id = $id AND artist_id = $artistId"
                        ))->count();

                        if($checkArtistHaveVideo <= 0)
                        {
                            $selectArtist = \Backend\Modules\Video\Models\Artist::findFirstById($artistId)->toArray();
                            if($selectArtist)
                            {
                                $videoArtistLink = new \Backend\Modules\Video\Models\VideoArtistLink();
                                $videoArtistLink->setVideoId($id);
                                $videoArtistLink->setArtistId($artistId);
                                if(!$videoArtistLink->save())
                                {

                                }
                            }
                        }
                    }
                }
                break;

            case 'video-group':
                if($this->request->getPost('selectArtist'))
                {
                    $id = $this->request->get('object_id');
                    
                    $videoGroup = \Backend\Modules\Video\Models\VideoGroup::findFirstById($id);
                    if(!$videoGroup)
                    {
                        return $this->response->redirect($this->request->getHTTPReferer());
                    }

                    $selectArtist = $this->request->getPost('selectArtist');

                    foreach($selectArtist as $artistId)
                    {
                        $checkArtistHaveVideo = \Backend\Modules\Video\Models\VideoArtistLink::find(array(
                            "video_group_id = $id AND artist_id = $artistId"
                        ))->count();

                        if($checkArtistHaveVideo <= 0)
                        {
                            $selectArtist = \Backend\Modules\Video\Models\Artist::findFirstById($artistId)->toArray();
                            if($selectArtist)
                            {
                                $videoArtistLink = new \Backend\Modules\Video\Models\VideoArtistLink();
                                $videoArtistLink->setVideoGroupId($id);
                                $videoArtistLink->setArtistId($artistId);
                                if(!$videoArtistLink->save())
                                {
                                    $this->flash->error($videoArtistLink->getMessages());
                                }
                            }
                        }
                    }
                }
                break;
        }
        return $this->response->redirect($this->request->getHTTPReferer());
	}
}